package com.example.androidframework.ui.main

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.WindowManager
import androidx.activity.OnBackPressedCallback
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.example.androidframework.MyApplication
import com.example.androidframework.R
import com.example.androidframework.databinding.ActivityMainBinding
import com.example.androidframework.ui.login.LoginActivity
import com.example.androidframework.ui.privacy.FirstPrivacyPopView
import com.lxj.xpopup.XPopup
import com.martin.lib_base.LibBase
import com.martin.lib_base.base.BaseActivity
import com.martin.lib_base.common.CommonVP2Adapter
import com.martin.lib_base.constant.ConstantKey
import com.martin.lib_base.ktx.loge
import com.tencent.mmkv.MMKV
import com.xuexiang.xui.utils.StatusBarUtils
import kotlinx.coroutines.launch

class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {

    override fun getViewModelClass(): Class<MainViewModel> = MainViewModel::class.java

    override fun layoutId(): Int = R.layout.activity_main

    override fun beforeSetContentView() {
        super.beforeSetContentView()
        // 沉浸式状态栏
        StatusBarUtils.translucent(this)
        // 屏幕常亮
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }

    override fun initOnCreate(savedInstanceState: Bundle?) {
        // 返回按钮监听
        onBackBtnPressed()

        binding.viewModel = viewModel

        binding.vp.apply {
            // 禁止滑动
            isUserInputEnabled = false
            // 设置屏幕外加载页面数量
//            offscreenPageLimit = viewModel.fragments.size - 1
            offscreenPageLimit = 1
            adapter = CommonVP2Adapter(
                viewModel.fragments, supportFragmentManager, lifecycle
            )
        }

        lifecycleScope.launch {
            viewModel.index.collect {
                // enum转Int
                val index = it.ordinal

                if (index < viewModel.fragments.size) {
                    if (index != 0 && index != 1) {
                        if (LibBase.isLogin.value == true) {
                            // 设置ViewPager的item
                            binding.vp.setCurrentItem(index, false)
                        } else {
                            // 登录
                            LoginActivity.start(this@MainActivity)
                            viewModel.tabChange(viewModel.preIndex.value)
                        }

                    } else {
                        // 设置ViewPager的item
                        binding.vp.setCurrentItem(index, false)
                    }


                    // 设置状态栏字体颜色
                    if (index == viewModel.fragments.size - 1) {
                        // 黑色
                        StatusBarUtils.setStatusBarDarkMode(this@MainActivity)
                    } else {
                        // 白色
                        StatusBarUtils.setStatusBarLightMode(this@MainActivity)
                    }


                } else {
                    loge("Tab数组越界")
                }
            }
        }

        if (!MMKV.defaultMMKV().getBoolean(ConstantKey.FIRST_PRIVACY, false)) {
            // 首次启动用户隐私协议弹窗
            XPopup.Builder(this).dismissOnTouchOutside(false).dismissOnBackPressed(
                false
            ).asCustom(FirstPrivacyPopView(this, onCanel = {
                finish()
            }, onSubmit = {
                MMKV.defaultMMKV().putBoolean(ConstantKey.FIRST_PRIVACY, true).commit()
            })).show()
        }
    }

    /// 返回按钮监听
    private fun onBackBtnPressed() {
        this.onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                viewModel.onBackPressed()
            }
        })
    }

    /// 返回按钮监听-已废弃
//    override fun onBackPressed() {
//        super.onBackPressed()
//        _viewModel.onBackPressed()
//    }

    override fun onPause() {
        super.onPause()
        // 黑色
        StatusBarUtils.setStatusBarDarkMode(this@MainActivity)
    }

    override fun onResume() {
        super.onResume()

        // 设置状态栏字体颜色
        if (viewModel.index.value.ordinal == viewModel.fragments.size - 1) {
            // 黑色
            StatusBarUtils.setStatusBarDarkMode(this@MainActivity)
        } else {
            // 白色
            StatusBarUtils.setStatusBarLightMode(this@MainActivity)
        }
    }

    override fun onStop() {
        super.onStop()

    }

    override fun showLife(): Boolean {
        return true
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val intent = Intent(context, MainActivity::class.java)
            context.startActivity(intent)
        }
    }
}